<template>
  <div id="client">
    <p style="margin-bottom: 20px">
      请输入：
      <el-input
        style="width: 200px; margin-left: 10px; margin-right: 10px"
        placeholder="请输入单据编号或客户名"
        v-model="ordernumorcname"
        clearable
      >
      </el-input>
      &nbsp;&nbsp;
      <span class="demonstration" style="margin-left: 20px">日期：</span>
      <el-date-picker
        v-model="youxiaodate"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="开始"
        end-placeholder="结束"
        :picker-options="pickerOptions"
        style="width: 180px"
        value-format="yyyy-MM-dd HH:mm:ss"
      >
      </el-date-picker>
      &nbsp;&nbsp;&nbsp; 销售人员：
      <el-select v-model="saler" placeholder="销售人员" style="width: 100px">
        <el-option
          v-for="item in salemen"
          :key="item.employeeId"
          :label="item.employeeName"
          :value="item.employeeCode"
        >
        </el-option>
      </el-select>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          收款状态<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-checkbox-group v-model="selectedOptions">
            <el-checkbox
              v-for="option in options"
              :label="option"
              :key="option"
              >{{ option }}</el-checkbox
            >
          </el-checkbox-group>
        </el-dropdown-menu>
      </el-dropdown>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
    </p>
    <p style="">
      &nbsp;&nbsp;&nbsp; 商品：<el-input
        style="width: 200px; margin-left: 10px; margin-right: 20px"
        placeholder="请输入商品"
        v-model="goodsList"
        clearable
      ></el-input>
      <el-button
        style="margin-left: -20px; margin-right: 10px"
        @click="goodsShow = true"
        >...</el-button
      >&nbsp;&nbsp;&nbsp; 审核状态
      <el-select
        v-model="auditStatus"
        placeholder="审核状态"
        style="width: 100px"
      >
        <el-option
          v-for="item in shenhe"
          :key="item.id"
          :label="item.name"
          :value="item.type"
        >
        </el-option>
      </el-select>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <el-button type="primary" @click="goselect()">查询</el-button>
    </p>
    <el-divider></el-divider>
    <div style="margin-bottom: 20px; margin-right: 20px; float: right">
      <el-button type="primary" @click="goto('SaleOrder')">新增</el-button
      >&nbsp;&nbsp;&nbsp;
      <!-- <el-button type="success">审核</el-button>&nbsp;&nbsp;&nbsp; -->
      <!-- <el-button type="warning">反审核</el-button>&nbsp;&nbsp;&nbsp; -->
      <el-button type="danger" @click="doRemove()">删除</el-button
      >&nbsp;&nbsp;&nbsp;
      <!-- <el-button type="primary">批量导入</el-button>&nbsp;&nbsp;&nbsp; -->
    </div>

    <div class="client-content">
      <el-table
        height="350px"
        :data="pageInfo.list"
        border
        style="width: 100%; font-size: 13px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="documentDate" label="单据日期" width="150">
        </el-table-column>
        <el-table-column prop="documentNum" label="单据编号" width="120">
        </el-table-column>
        <el-table-column prop="orderNum" label="关联源销货单号" width="120">
        </el-table-column>
        <el-table-column
          prop="returnNumber"
          label="关联销货退货单号"
          width="120"
        >
        </el-table-column>
        <el-table-column prop="customerNum" label="客户编号" width="120">
        </el-table-column>
        <el-table-column label="地址" width="120">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.address"
              placeholder="地址"
              style="width: 150px"
            >
              <el-option
                v-for="item in addresses"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                disabled
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column prop="totalMoney" label="销售金额（元）" width="120">
        </el-table-column>
        <el-table-column prop="received" label="已收款" width="120">
        </el-table-column>
        <el-table-column prop="unpaid" label="收款状态" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.unpaid === 0">部分收款</span>
            <span v-if="scope.row.unpaid === 1">未收款</span>
            <span v-if="scope.row.unpaid === 2">全部收款</span>
          </template>
        </el-table-column>
        <el-table-column prop="unreturned" label="退货状态" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.unreturned === 0">全部退款</span>
            <span v-if="scope.row.unreturned === 1">部分退款</span>
            <span v-if="scope.row.unreturned === 2">未退款</span>
          </template>
        </el-table-column>
        <el-table-column prop="payDueDate" label="收款到期日" width="120">
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="120">
        </el-table-column>
        <el-table-column prop="logisticsNum" label="关联物流订单号" width="120">
        </el-table-column>
        <el-table-column prop="status" label="审核状态" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.status === 0">已保存</span>
            <span v-if="scope.row.status === 1">已提交</span>
            <span v-if="scope.row.status === 2">已审核</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleClick(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-pagination
      style="position: relative; left: 700px; top: 30px"
      @current-change="handleCurrentChange"
      :page-size="pageInfo.pageSize"
      layout="prev, pager, next, jumper"
      :total="pageInfo.total"
    ></el-pagination>
    <el-dialog top="20px" title="选择仓库" :visible.sync="dialogFormVisible">
      <el-input
        v-model="input"
        placeholder="请输入编号或名称"
        style="width: 200px; margin-left: 20px"
      ></el-input>
      <el-checkbox v-model="checked" style="margin-left: 20px"
        >显示禁用仓库</el-checkbox
      >
      <el-button type="success" style="margin-left: 20px">查询</el-button>
      <el-table :data="gridData">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          width="100"
          property="id"
          label="编号"
        ></el-table-column>
        <el-table-column
          width="100"
          property="name"
          label="名称"
        ></el-table-column>
      </el-table>
      <div class="block">
        <span class="demonstration"></span>
        <el-pagination layout="prev, pager, next" :total="1000">
        </el-pagination>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog top="20px" title="选择商品" :visible.sync="goodsShow">
      <GoodsChoose ref="chooseGoods" :goodsShow="goodsShow" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="goodsShow = false">取 消</el-button>
        <el-button type="primary" @click="yesGoods">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import GoodsChoose from "../public_com/GoodsChoose.vue";

export default {
  name: "SaleOrderList",
  components: {
    GoodsChoose,
  },
  data() {
    return {
      stockId: "",
      storeList: [],
      ordernumorcname: "",
      inBoundList: [],
      salemen: [],
      goodsShow: false,
      auditStatus: "",
      addresses: [
        { id: 1, name: "地址1" },
        { id: 2, name: "地址2" },
        { id: 3, name: "地址3" },
      ],
      goods: "",
      ck: "",
      gridData1: [],
      dialogFormVisible1: false,
      goodsShow: false,
      dialogFormVisible: false,
      checkAll: false,

      inBoundList: [],
      saler: "",
      shenhe: [
        {
          id: 3,
          type: "",
          name: "全部",
        },
        {
          id: 2,
          type: 2,
          name: "已审核",
        },
      ],
      youxiaodate: "",
      zonginput: "",
      selectedOptions: [],
      multipleSelection: [],
      goodname: "",
      options: ["未收款", "部分收款", "已收款"],

      isIndeterminate: false,
      dialogFormVisible: false,
      pageInfo: "",
      ids: [],
    };
  },
  computed: {
    goodsList() {
      var list = "";
      this.inBoundList.forEach((item) => {
        list = list + "|" + item.goodname;
      });
      this.goodname = list;
      return list;
    },
  },
  methods: {
    getStoreList() {
      this.axios
        .get("/info/storeHouse/getAllStore", {
          params: {
            cId: 1,
          },
        })
        .then((rs) => {
          this.storeList = rs.data.storeList;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    goselect() {
      this.getList(1);
    },
    getSaler() {
      this.axios({
        url: "/info/employee/listAll",
      })
        .then((rs) => {
          this.salemen = rs.data.list;
          console.log("saleMan", rs.data);
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleClick(row) {
      console.log(row);
      const id = row.id;
      this.$router.push({ name: "SaleOrder", params: { id: id } });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.ids = this.multipleSelection.map((item) => item.id);
    },
    doRemove() {
      this.axios({
        url: "/erp/sale/salesOrder/delete",
        method: "POST",
        data: this.ids, // 直接传递 ids 数组
      }).then((rs) => {
        console.log("isDel", rs.data);
        if (rs.data.status == true) {
          alert("删除成功！");
          this.getList(1);
        } else {
          alert("删除失败！");
        }
      });
    },
    handleCurrentChange(val) {
      this.getList(val);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    yesGoods() {
      this.goodsShow = false;
      // this.goodsList =
      // console.log(this.goodsList)
      //遍历商品列表
      this.inBoundList = this.$refs.chooseGoods.selectionList;
      console.log(this.inBoundList);
      this.inBoundList.forEach((item) => {
        this.gridData1.push({
          gid: item.gid,
          type: 1,
          goodname: item.goodname,
          unitname: item.unitname,
          goodcode: item.goodcode,
          unitId: item.unitId,
          stockId: item.stockId,
          totalNum: item.totalNum,
          totalPrice: item.totalPrice,
          tax: item.tax,
          discount: item.discount,
          giveGoods: item.giveGoods,
          unitPrice: item.unitPrice,
        });
        console.log("gridData1", this.gridData1);
      });
    },
    getList(pageNum) {
      var saler = this.saler;
      var startTime = this.youxiaodate[0];
      var endTime = this.youxiaodate[1];
      var ordernumorcname = this.ordernumorcname;
      console.log("gridData1", this.gridData1);
      var paymentStatus = [];
      this.selectedOptions.forEach((item) => {
        if (item == "部分收款") {
          paymentStatus.push(0);
        }
        if (item == "未收款") {
          paymentStatus.push(1);
        }
        if (item == "全部收款") {
          paymentStatus.push(2);
        }
      });
      var auditStatus = this.auditStatus;
      var dataForm = new FormData();
      dataForm = {
        goodname: this.goodname,
        saler,
        startTime,
        endTime,
        ordernumorcname,
        auditStatus,
        stockId: this.stockId,
        paymentStatus: JSON.stringify(paymentStatus),
        pageNum,
        pageSize: 5,
      };
      this.axios({
        url: "/erp/sale/salesOrder/byPage",
        method: "POST",
        data: dataForm,
      })
        .then((rs) => {
          this.getSaler();
          this.pageInfo = rs.data.pageInfo;
          console.log("salesorder", this.pageInfo.list);
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleCheckedCitiesChange(value) {
      const checkedCount = value.length;
      this.checkAll = checkedCount === this.options.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.options.length;
    },
    goto(to) {
      if (this.$route.path !== "/" + to) {
        this.$router.push(to);
      }
    },
  },
  created() {
    this.getStoreList();
    this.getList(1);
  },
  watch: {
    selectedOptions(value) {
      this.handleCheckedCitiesChange(value);
    },
  },
};
</script>

<style scoped>
.el-button el-button--default el-popover__reference {
  margin-right: 20px;
}

tr {
  border: 1px solid black;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>